package com.canbot.u05.utils;

import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collections;
import java.util.List;

public class Utils {

        /**
         * 从sd卡上获得bitmap图片
         *
         * @param imageName
         * @return
         */
        public static Bitmap fromSD(String sd, String imageName) {
                String path = sd + imageName;
                if (FileUtil.fileIsExists(path)) {
                        return BitmapFactory.decodeFile(path);
                }
                else {
                        return null;
                }
        }

        /**
         * 从sd卡上获得bitmap图片
         *
         * @param path
         * @return
         */
        public static Bitmap fromSD(String path) {
                if (FileUtil.fileIsExists(path)) {
                        return BitmapFactory.decodeFile(path);
                }
                else {
                        return null;
                }
        }

        /**
         * 判断进程是否运行
         *
         * @return
         */
        public static boolean isProessRunning(Context context, String proessName) {
                boolean isRunning = false;
                ActivityManager am = (ActivityManager) context
                        .getSystemService(Context.ACTIVITY_SERVICE);

                List<RunningAppProcessInfo> lists = am.getRunningAppProcesses();
                for (RunningAppProcessInfo info : lists) {
                        if (info.processName.equals(proessName)) {
                                isRunning = true;
                        }
                }

                return isRunning;
        }

        public static boolean matchString(String src, String dest[]) {
                int len = dest.length;
                if (src == null || dest == null || len == 0) {
                        return false;
                }
                if (src != null && src.equals("")) {
                        return false;
                }
                src = src.trim();
                for (int i = 0; i < len; i++) {
                        String mDest = dest[i].trim();
                        if (src.contains(mDest)) {
                                return true;
                        }
                        else {
                                continue;
                        }
                }
                return false;
        }


        /**
         * 获取设备的MAC地址，必须打开WIFI，否则获取不到
         *
         * @return
         */
        public static String getMac() {
                String macSerial = "";
                try {
                        Process pp = Runtime.getRuntime().exec(
                                "cat /sys/class/net/wlan0/address");
                        InputStreamReader ir = new InputStreamReader(pp.getInputStream());
                        LineNumberReader input = new LineNumberReader(ir);
                        String line;
                        while ((line = input.readLine()) != null) {
                                macSerial += line.trim();
                        }
                        input.close();
                }
                catch (IOException e) {
                        e.printStackTrace();
                }
                if (null != macSerial && macSerial.contains(":")) {
                        macSerial = macSerial.replace(":", "");
                }
                return macSerial;
        }

        /**
         * 获取设备的本地MAC地址，必须打开WIFI，否则获取不到
         *
         * @param context
         * @return
         */
        public static String getLocalMacAddressFromIp(Context context) {
                String mac_s = "";
                try {
                        byte[] mac;
                        NetworkInterface ne = NetworkInterface.getByInetAddress(InetAddress.getByName(getLocalIpAddress()));
                        mac = ne.getHardwareAddress();
                        mac_s = byte2hex(mac);
                }
                catch (Exception e) {
                        e.printStackTrace();
                }
                return mac_s;
        }

        public static String byte2hex(byte[] b) {
                StringBuffer hs = new StringBuffer(b.length);
                String stmp = "";
                int len = b.length;
                for (int n = 0; n < len; n++) {
                        stmp = Integer.toHexString(b[n] & 0xFF);
                        if (stmp.length() == 1)
                                hs = hs.append("0").append(stmp);
                        else {
                                hs = hs.append(stmp);
                        }
                }
                return String.valueOf(hs);
        }


        public static String getLocalIpAddress() {
                try {
                        String ipv4;
                        List<NetworkInterface> nilist = Collections.list(NetworkInterface.getNetworkInterfaces());
                        for (NetworkInterface ni : nilist) {
                                List<InetAddress> ialist = Collections.list(ni.getInetAddresses());
                                for (InetAddress address : ialist) {
                                        if (!address.isLoopbackAddress() && address instanceof Inet4Address) {
                                                ipv4 = address.getHostAddress();
                                                Logger.e("ipv4", ipv4);
                                                if (!"192.168.12.1".equals(ipv4)) {
                                                        return ipv4;
                                                }
                                        }
                                }
                        }
                }
                catch (SocketException ex) {
                        ex.printStackTrace();
                }
                return null;
        }
}
